package com.itheima.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.cargo.ContractDao;
import com.itheima.dao.cargo.ContractProductDao;
import com.itheima.dao.cargo.ExtCproductDao;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.ContractProductService;
import com.itheima.vo.ContractProductVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service(timeout = 100000) // 如果要调试需要设置超时时间
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public PageInfo<ContractProduct> findByPage(ContractProductExample contractProductExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ContractProduct> list = contractProductDao.selectByExample(contractProductExample);
        return new PageInfo<ContractProduct>(list);
    }

    @Override
    public List<ContractProduct> findAll(ContractProductExample contractProductExample) {
        return contractProductDao.selectByExample(contractProductExample);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    /**
     * 添加货物
     *
     * @param contractProduct
     */
    @Override
    public void save(ContractProduct contractProduct) {
        // 设置id
        contractProduct.setId(UUID.randomUUID().toString());
        //A. 计算amount，货物金额 = 单价 * 数量
        Double amount = 0d;
        if (contractProduct.getCnumber() != null && contractProduct.getPrice() != null) {
            amount = contractProduct.getCnumber() * contractProduct.getPrice();
        }
        contractProduct.setAmount(amount);

        //B. 修改购销合同，总金额 = 总金额 + 货物金额
        //B1. 根据购销合同id，查询
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //B2. 修改总金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);

        //C. 修改购销合同，货物数量 = 原有货物数量 + 1
        contract.setProNum(contract.getProNum() + 1);
        // 修改购销合同
        contractDao.updateByPrimaryKeySelective(contract);

        //添加货物
        contractProductDao.insertSelective(contractProduct);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //A. 计算修改后的货物金额
        Double amount = 0d;
        if (contractProduct.getCnumber() != null && contractProduct.getPrice() != null) {
            amount = contractProduct.getCnumber() * contractProduct.getPrice();
        }
        contractProduct.setAmount(amount);


        //B. 根据货物id，查询货物对象，获取数据库中货物金额，就是修改前的货物金额
        ContractProduct cp =
                contractProductDao.selectByPrimaryKey(contractProduct.getId());
        // 获取修改前的货物金额
        Double oldAmount = cp.getAmount();


        //C. 修改购销合同，总金额 = 总金额 + 修改后 - 修改前
        Contract contract =
                contractDao.selectByPrimaryKey(contractProduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() + amount - oldAmount);
        contractDao.updateByPrimaryKeySelective(contract);

        // 修改货物
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    @Override
    public void delete(String id) {
        //1.根据货物id，查询获取货物金额
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        Double amount = contractProduct.getAmount();

        //2.根据货物id，查询所有附件，遍历累计附件金额; 删除附件
        ExtCproductExample exmpale = new ExtCproductExample();
        // SELECT * FROM co_ext_cproduct WHERE contract_product_id=''
        exmpale.createCriteria().andContractProductIdEqualTo(id);
        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(exmpale);
        // 累计附件金额
        Double extAmount = 0d;
        if (extCproductList != null && extCproductList.size() > 0) {
            // 遍历附件
            for (ExtCproduct extCproduct : extCproductList) {
                extAmount += extCproduct.getAmount();
                // 同时，删除附件
                extCproductDao.deleteByPrimaryKey(extCproduct.getId());
            }
        }

        //3.修改购销合同
        Contract contract =
                contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //总金额 = 总金额 - 货物金额  - 附件金额  【..........最重要..........】
        contract.setTotalAmount(contract.getTotalAmount() - amount - extAmount);
        //修改货物数量
        contract.setProNum(contract.getProNum() - 1);
        //修改附件数量
        contract.setExtNum(contract.getExtNum() - extCproductList.size());
        // 修改
        contractDao.updateByPrimaryKeySelective(contract);

        //4. 最终：删除货物
        contractProductDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<ContractProductVo> findByShipTime(String companyId, String inputDate) {
        return contractProductDao.findByShipTime(companyId, inputDate);
    }
}
